[C] Programming problem: Storing values of an array in one variable

Posted by OldMacDonald on Stack Overflow See other posts from Stack Overflow or by OldMacDonald
Published on 2010-06-02T11:41:33Z Indexed on 2010/06/02 11:43 UTC
Read the original article Hit count: 164

Filed under:
|
|
|

Hello,

I am trying to use md5 code to calculate checksums of file. Now the given function prints out the (previously calculated) checksum on screen, but I want to store it in a variable, to be able to compare it later on.

I guess the main problem is that I want to store the content of an array in one variable. How can I manage that? Probably this is a very stupid question, but maybe somone can help.

Below is the function to print out the value. I want to modify it to store the result in one variable.

static void MDPrint (mdContext)
MD5_CTX *mdContext;
{
 int i;
 for (i = 0; i < 16; i++)
 {
   printf ("%02x", mdContext->digest[i]);
 } // end of for 
} // end of function

For reasons of completeness the used struct: /* typedef a 32 bit type */ typedef unsigned long int UINT4;

/* Data structure for MD5 (Message Digest) computation */
typedef struct {
 UINT4 i[2];                   /* number of _bits_ handled mod 2^64 */
 UINT4 buf[4];                                    /* scratch buffer */
 unsigned char in[64];                              /* input buffer */
 unsigned char digest[16];     /* actual digest after MD5Final call */
} MD5_CTX;

and the used function to calculate the checksum:

static int MDFile (filename)
char *filename;
{
 FILE *inFile = fopen (filename, "rb");
 MD5_CTX mdContext;
 int bytes;
 unsigned char data[1024];
 if (inFile == NULL) {
    printf ("%s can't be opened.\n", filename);
    return -1;
 } // end of if
 MD5Init (&mdContext);
 while ((bytes = fread (data, 1, 1024, inFile)) != 0)
 MD5Update (&mdContext, data, bytes);
 MD5Final (&mdContext);
 MDPrint (&mdContext);
 printf (" %s\n", filename);
 fclose (inFile);
 return 0;
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about array